/**
 * Copyright (c) www.bugull.com
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ebocy.hammer.core.db.mongo.backend;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;

import com.ebocy.hammer.core.db.IEntity;
import com.ebocy.hammer.core.db.mongo.MongoDao;
import com.ebocy.hammer.core.db.mongo.cache.MongoDaoCache;
import com.ebocy.hammer.core.db.mongo.cache.IndexWriterCache;
import com.ebocy.hammer.core.db.mongo.mapper.MapperUtil;

/**
 *
 * @author Frank Wen(xbwen@hotmail.com)
 */
public class IndexRebuildTask implements Runnable{

    private final static Logger logger = Logger.getLogger(IndexRebuildTask.class);

    private Class<?> clazz;
    private IndexWriter writer;
    private int batchSize;

    public IndexRebuildTask(Class<?> clazz, int batchSize){
        this.clazz = clazz;
        this.batchSize = batchSize;
        String name = MapperUtil.getEntityName(clazz);
        IndexWriterCache cache = IndexWriterCache.getInstance();
        writer = cache.get(name);
    }

    @Override
    public void run() {
        try{
            writer.deleteAll();
        }catch(IOException ex){
            logger.error("Something is wrong when lucene IndexWriter doing deleteAll()", ex);
        }
        MongoDao dao = MongoDaoCache.getInstance().get(clazz);
        long count = dao.count();
        int pages = (int) (count / batchSize);
        int remainder = (int) (count % batchSize);
        if(pages > 0){
            for(int i=1; i<=pages; i++){
                List list = dao.findForLucene(i, batchSize);
                process(list);
            }
        }
        if(remainder > 0){
            List list = dao.findForLucene(++pages, remainder);
            process(list);
        }
    }

    private void process(List list){
        for(Object o : list){
            IEntity obj = (IEntity)o;
            process(obj);
        }
    }

    private void process(IEntity obj){
        IndexFilterChecker checker = new IndexFilterChecker(obj);
        if(checker.needIndex()){
            Document doc = new Document();
            IndexCreator creator = new IndexCreator(obj, "");
            creator.create(doc);
            try {
                writer.addDocument(doc);
            } catch (CorruptIndexException ex) {
                logger.error("IndexWriter can not add a document to the lucene index", ex);
            } catch (IOException ex) {
                logger.error("IndexWriter can not add a document to the lucene index", ex);
            }
        }
    }

}
